package com.hb.trade.api.im.chat.controller;


import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hb.core.pojo.im.entity.ChatRecordContent;
import com.hb.trade.api.im.chat.service.ChatRecordContentService;
import com.hb.trade.api.im.chat.service.ChatUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.LayuiResponseDto;
import org.fzzn.core.model.user.layim.LayImUser;
import org.fzzn.core.model.user.login.SimpleUserExample;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * 聊天记录表 前端控制器
 *
 * @author zhaojk
 * @since 2021-12-30
 */
@Slf4j
@Version(1.0)
@Api(tags = {"layim即时通讯"})
@RequestMapping("/{version}")
@RestController
public class ChatRecordController {

    protected final ChatUserService chatUserService;
    protected final ChatRecordContentService chatRecordContentService;

    public ChatRecordController(ChatUserService chatUserService,
                                ChatRecordContentService chatRecordContentService) {
        this.chatUserService = chatUserService;
        this.chatRecordContentService = chatRecordContentService;
    }

    /**
     * 查看当前用户与指定用户(或指定群组的)的聊天记录
     *
     * @param recordContent id   前端参数, 指定用户(或指定群组的)id, 关联表(im_chat_user, im_chat_group)
     *                      type
     * @return
     */
    @ApiOperation(value = "查看当前用户与指定用户(或指定群组的)的聊天记录", notes = "查看当前用户与指定用户(或指定群组的)的聊天记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "前端参数, 指定用户(或指定群组的)id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type", value = "消息类型('friend': 好友私聊消息, 'group': 群聊消息)", dataTypeClass = Long.class)
    })
    @PostMapping("/chatRecord/allRecord")
    public LayuiResponseDto allRecord(@RequestBody ChatRecordContent recordContent) {
        if (recordContent.getId() == null || StrUtil.isEmpty(recordContent.getType())) {
            return ResultUtil.layData(-1, MsgEnum.INCORRECT_PARAM.msg(), 0, null);
        }
        // 获取当前登录用户的LayimUser对象
        SimpleUserExample currentUser = ShiroUtil.getUserEntity();
        LayImUser fromLayUser = chatUserService.getLayimMine(currentUser.getUsername());
        if (fromLayUser == null) {
            return ResultUtil.layData(-1, "未查询到相关用户信息", 0, null);
        }

        // 查询全部聊天记录
        recordContent.setFromId(NumberUtil.parseLong(fromLayUser.getId()));
        recordContent.setToId(recordContent.getId());
        Page<LayImUser> recordPageData = chatRecordContentService.historyRecord(recordContent);
        if (recordPageData == null) {
            return ResultUtil.layData(-1, "没有任何聊天记录", 0, null);
        }
        return ResultUtil.layData(0, "", recordPageData.getTotal(), recordPageData);
    }
}
